<?php
/**
  * @company     xingquan
  * @auther  BianYuan
  * @date     Tue Dec 04 16:47:32 CST 2007
  * @copyright  xingQuan
  * @file        db.class.php
  * @todo   Mysql class
  * @example  *********************************************************************
  *     * $db = new Dbclass('host','user','pass','dbname', true); *
  *    * $db->connect();                                                 *
  *    * print_r($db->getAll("SELECT * FROM fj_area"));                  *
  *     *********************************************************************
  * @version   0.1
*/
if(!defined('IN_SITE')) {
  //exit('Access Denied');
}
class Dbclass {
  // Database connect parameter
  var $host, $user, $pass, $dbname;
  var $link;
  var $halt;
  /**
   * @desc Db constructor function
   * @param  String $db_host
   * @param  String  $db_user
   * @param  String  $db_pass
   * @param  String  $db_name
   * @param  Boolean $halt
   * @return  None
   * */
  function Dbclass($dsn=array(), $halt=true) {
   $this->host   = $dsn['host'];
   $this->user   = $dsn['user'];
   $this->pass   = $dsn['pass'];
   $this->dbname = $dsn['db'];
   $this->halt   = $halt;
   $this->connect();
  }
  /**
   * @desc Connect db method
   * @param  Boolean $pconnect
   * @param  String  $charset
   * @return  Resource
   * */
  function connect($pconnect=false, $charset='utf8') {
   if($pconnect) {// @notice: If want use transaction do not use pconnect
    $this->link = @ mysql_pconnect($this->host, $this->user, $this->pass);
   }else {
    $this->link = @ mysql_connect($this->host, $this->user, $this->pass);
   }
   if($this->halt && !$this->link) {
    die($this->error());
   }
   if($this->version() > '4.1') {
    @ mysql_query("SET NAMES '".$charset."' ", $this->link);
   }
   if($this->dbname) {
    @ mysql_select_db($this->dbname, $this->link);
   }
  }
  /**
   * @desc  Select DB
   * @param  String $dbname
   * @return  Resource
   * */
  function selectDb($dbname) {
   return mysql_select_db($dbname, $this->link);
  }
  /**
   * @desc Db query
   * @param  String  $sql
   * @param  Bllean $buffer
   * @return  Result
   * */
  function query($sql, $buffer=true) {
   $query_func = $buffer ? 'mysql_query' : 'mysql_unbuffered_query';
   if(!is_array($sql)) {
    $query = @ $query_func($sql, $this->link);
    if($this->halt && !$query){
     die($this->error());
    }
    return $query;
   }
   else {
    foreach ($sql as $q) {
     $query = @ $query_func($q, $this->link);
     if($this->halt && !$query) {
      die($this->error());
     }
    }
    return $query;
   }
  }
  /**
   * @desc Get array of query
   * @param  String  $query
   * @param  String  $result_type
   * @return  Array
   * */
  function fetchArray($query, $result_type=MYSQL_ASSOC) {
   return @ mysql_fetch_array($query, $result_type);
  }
  /**
   * @desc Get preview mysql operated affected rows
   * @param  None
   * @return  Integer
   * */
  function affectedRows() {
   return mysql_affected_rows($this->link);
  }
  /**
   * @desc Get preview mysql select record num
   * @param  Resource $rs
   * @return  Integer
   * */
  function numRows($rs) {
   return mysql_numrows($rs);
  }
  /**
   * @desc Total table record count
   * @param  String $table
   * @return  Integer
   * */
  function totalRecords($table) {
   $query = $this->query("SELECT COUNT(*) as total FROM $table");
   if($this->halt && !$query){
    die($this->error());
   }else{
    $row = $this->fetchArray($query);
    return $row['total'];
   }
  }
  /**
   * @desc Get one record from query
   * @param  String $sql
   * @return  Array
   * */
  function getRow($sql) {
           $query = $this->query($sql);
           if($this->halt && !$query){
            die($this->error());
           }
           return $this->fetchArray($query);
       }
       /**
        * @desc get All record from some table
        * @param  String $sql
        * @return  Array
        * */
       function getAll($sql) {
        $arReturn = array();
        $query = $this->query($sql,false);
        if($this->halt && !$query){
            die($this->error());
           }
           while($row = $this->fetchArray($query)) {
            $arReturn[] = $row;
           }
           return $arReturn;
       }
       /**
        * @desc Get limit result
        * @param  String $table
        * @param  Integer $offset
        * @param  Integer $rows
        * @param  String  $condiction
        * @param  String  $order
        * */
       function limitQuery($table, $offset=0, $rows=10, $condiction='', $order='') {
        $arReturn = array();
        $sql = "SELECT * FROM `" . $table . "`";
        $condiction != '' ? ($sql .= " WHERE $where") : $sql .= '';
        $order != '' ? ($sql .= " ORDER BY $order") : $sql .= '';
        $sql .= " LIMIT $offset, $rows";
        $query = $this->query($sql);
        while($row = $this->fetchArray($query)) {
         $arReturn[] = $row;
        }
        return $arReturn;
       }
  /**
   * @desc Get error info
   * @param  None
   * @return  String
   * */
  function error() {
   return (($this->link) ? mysql_error($this->link) 
          : mysql_errno() . " : " .mysql_error());
  }
  /**
   * @desc Free query result
   * @param  Resource $rs
   * @return  Boolean
   * */
  function freeResult($rs) {
   return mysql_free_result($rs);
  }
  /**
   * @desc Return preview insert id
   * @param  None
   * @return  Integer
   * */
  function lastInsertId() {
   return mysql_insert_id($this->link);
  }
  /**
   * @desc String filter
   * @param  String $str
   * @return  String
   * */
     function escape($str) {
         if(function_exists('mysql_real_escape_string')) {
             return @mysql_real_escape_string($str, $this->link);
         } else {
             return @mysql_escape_string($str);
         }
     }  
  /**
   * @desc Get mysql version info
   * @param  None
   * @return  String
   * */
  function version() {
   return @ mysql_get_server_info($this->link);
  }
  /**
   * @desc Close mysql connect
   * @param  None
   * @return  Boolean
   * */
  function close() {
   $ret = mysql_close($this->link);
   $this->link = null;
   return $ret;
  }
}

?>
